/*
 * launcher_robot12.cpp
 *
 */

#include "../middleware/CommunicationDespacho.h"
#include "../middleware/CommunicationRobot16.h"
#include "../middleware/CommunicationAdpt.h"
#include "../channel_common/AgentIdManager.h"
#include "../channel_common/ChannelInterface.h"
#include "../IPC/ProcessAux.h"
#include "../common/Argv.h"
#include "../capa_servicios_distribuidos/QueueLauncher.h"
#include "../middleware/CommunicationVendedor.h"
#include "../control/clienteCommon.h"
#include "../application/AdminKeys.h"
#include "../middleware/CommunicationAGV.h"
#include "../middleware/CommunicationCanastos.h"
#include "../middleware/CommunicationRobot11.h"
#include "../middleware/CommunicationRobot12.h"
#include "../distributed_semaphore/comun/DSEMManager.h"
#include "../distributed_semaphore/comun/DSEMCommon.h"

using namespace middleware;

int main(int argc, char**argv)
{
	CLogger::create("Launcher del Robot 12");
	std::string hostName = "robot12_pc";

	Argv args;
	args.pushArg(hostName);

	channel::ChannelInterface::create(hostName);

	ProcessAux::launchProcess2("./out/channel_out_agente",1,args);
	ProcessAux::launchProcess2("./out/channel_in_agente",1,args);

	AdminKeys adminZonaIzq(ZONA_IZQ);
	AdminKeys adminZonaMedio(ZONA_MED);
	AdminKeys adminZonaDer(ZONA_DER);

	CLogger::log("Creando AGV");
	try {
		CommunicationAGV::create(adminZonaIzq.getAgvKey(), CommunicationAGV::R12,hostName);
		CommunicationAGV::create(adminZonaDer.getAgvKey(), CommunicationAGV::R12,hostName);
		CommunicationAGV::create(adminZonaMedio.getAgvKey(), CommunicationAGV::R12,hostName);
	} catch (Exception e) {CLogger::log(e.what(), LogLevelEnum::FATAL, color::WHITE);}

	CLogger::log("Creando canastos");
	try {
		CommunicationCanastos::create(adminZonaIzq.getCanastosKey());
		CommunicationCanastos::create(adminZonaDer.getCanastosKey());
		CommunicationCanastos::create(adminZonaMedio.getCanastosKey());
	} catch (Exception e) {CLogger::log(e.what(), LogLevelEnum::FATAL, color::WHITE);}

	CLogger::log("Creando robot 11");
	try {
		CommunicationRobot11::create(adminZonaIzq.getRobot11Key(), CommunicationRobot11::ROBOT12);
		CommunicationRobot11::create(adminZonaDer.getRobot11Key(), CommunicationRobot11::ROBOT12);
	} catch (Exception e) {CLogger::log(e.what(), LogLevelEnum::FATAL, color::WHITE);}

	CLogger::log("Creando robot 12");
	try {
		CommunicationRobot12::create(adminZonaMedio.getLeftRobot12Key(), CommunicationRobot12::ROBOT12);
		CommunicationRobot12::create(adminZonaMedio.getRightRobot12Key(), CommunicationRobot12::ROBOT12);
	} catch (Exception e) {CLogger::log(e.what(), LogLevelEnum::FATAL, color::WHITE);}

	CLogger::log("Creando primer cinta");
	try {
		CommunicationPrimerCinta::create(adminZonaIzq.getCintaKey());
		CommunicationPrimerCinta::create(adminZonaDer.getCintaKey());
	} catch (Exception e) {CLogger::log(e.what(), LogLevelEnum::FATAL, color::WHITE);}

	/* Semaphore signalers and waiters */
		if (DSEM_ENABLED) {
			/* Wait a su semaforo (izq y der) */
			DSEMManager::createWaiter(
					CommunicationRobot12::getSemRobot12FileWait(),
					adminZonaMedio.getLeftRobot12Key(),
					CommunicationRobot12::getSemControlName()+"izq");
			DSEMManager::createWaiter(
					CommunicationRobot12::getSemRobot12FileWait(),
					adminZonaMedio.getRightRobot12Key(),
					CommunicationRobot12::getSemControlName()+"der");

			/* Signal al semaforo de Robot12 listo (izq y der) */
			DSEMManager::createSignaler(
					CommunicationRobot11::getSemRobot11FileSignal(),
					CommunicationRobot11::getSemEsperaRobot12Key(adminZonaIzq.getRobot11Key()),
					CommunicationRobot11::getSemEspera12Name()+"izq");
			DSEMManager::createSignaler(
					CommunicationRobot11::getSemRobot11FileSignal(),
					CommunicationRobot11::getSemEsperaRobot12Key(adminZonaDer.getRobot11Key()),
					CommunicationRobot11::getSemEspera12Name()+"der");
		}
}
